// 创建应用
const path = require('path')
const jsonServer = require('json-server')
const router = require('./router')
const db = require('./db')()

const server = jsonServer.create()

const middlewares = jsonServer.defaults({
  static: path.join(__dirname, '../public')
})

// 使用中间件
server.use(middlewares)

server.use(jsonServer.bodyParser)

server.use((req, res, next) => {
  const json = res.json.bind(res)
  res.success = data => {
    return json({
      code: 0,
      msg: '请求成功',
      data
    })
  }
  res.fail = (msg, code = -1, data) => {
    return json({
      code,
      msg,
      data
    })
  }
  next()
})

router(server)

// 延时中间件让请求慢一点
server.use((req, res, next) => {
  setTimeout(next, 1000)
})

const jsonRouter = jsonServer.router(db)
server.use('/api', jsonRouter)

server.listen(8000, () => {
  console.log('Json Server is runnning 8000')
})
